//
//  WBNavigationController.swift
//  个人微博
//
//  Created by 任聪聪 on 2017/5/16.
//  Copyright © 2017年 任孜聪. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        //只有当vc不是根控制器的时候才进行判断隐藏，根控制器不做处理
        //viewController 是被push的控制器，设置返回按钮可以设置他的left按钮
        var title = "返回"
        if childViewControllers.count>0{
            viewController.hidesBottomBarWhenPushed = true
            //判断控制器类型
            if let vc = viewController as? WBBaseViewController{
                for item in childViewControllers {
                    // 判断控制器级别，只有一个子控制器时候，显示栈底控制器标题
//                    if childViewControllers.count == 1{
//                        title = childViewControllers.first?.title ?? "返回"
//                    }
                    title = item.title ?? "返回"
                }
                vc.navBarItem.leftBarButtonItem = UIBarButtonItem(title: title, any: self, action: #selector(getBack),isBack:true)
            }
        }
        super.pushViewController(viewController, animated: true)
    }
    //返回上一级
    @objc fileprivate func getBack(){
        popViewController(animated: true)
    }
}
